package jtwittconsolepackage;

import java.util.HashMap;

/**
 * The ColorManager holds all available colors for a linux console.
 * Moreover it contains a hashmap which provides a relation between twitter users
 * and there actual color.
 */
public class ColorManager
{
    //codes for all available foreground colors
    private final String[] colorArray =
    {
        "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m"
    };
    //the color code that was used for the latest added user
    private int latestUsedColor = 0;
    private HashMap<String, String> colorHashMap = new HashMap<String, String>();

    /**
     * Returns the string to color a nickname (if the given nickname is allready
     * in the hasmap).
     * If the nickname is not available in the hashmap a new color will be
     * defined for the specified user and returned.
     * @param username the username which should be coloured in the output
     * @return the machting color
     */
    public String getColor(String username)
    {
        //if the nickname can not be found in the hashmap then add a new color
        //value for it
        if (!colorHashMap.containsKey(username))
        {
            //add the nickname and the color value
            colorHashMap.put(username, colorArray[latestUsedColor]);
            //next added username gets the next color within the array
            latestUsedColor = (latestUsedColor + 1) % (colorArray.length - 1);
        }

        return colorHashMap.get(username);
    }
}
